package org.unidbfw.utils;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.unidbfw.common.UniDBFWException;
import org.unidbfw.db.ConnectionHelper;

public class QueryUtils
{
	
	public static String generateDeleteQuery(String tableName) throws UniDBFWException
	{
		StringBuilder sb = new StringBuilder("delete from ");
		sb.append(tableName);
		sb.append(" where ");
		boolean isFirst = true;
		for (String column: getColumns(tableName))
		{
			if (!isFirst)
			{
				sb.append(" and ");
			}
			else
			{
				isFirst = false;
			}
			sb.append(column);
			sb.append(" = ? ");
		}
		return sb.toString();
	}
	
	public static String generateInsertQuery(String tableName) throws UniDBFWException
	{
		List<String> columns = getColumns(tableName);
		StringBuilder sb = new StringBuilder("insert into ");
		sb.append(tableName);
		sb.append("(");
		boolean isFirst = true;
		for (String column: columns)
		{
			if (!isFirst)
			{
				sb.append(", ");
			}
			else
			{
				isFirst = false;
			}
			sb.append(column);
		}
		sb.append(") values( ");
		isFirst = true;
		for (int i = 0; i < columns.size(); i++)
		{
			if (!isFirst)
			{
				sb.append(", ");
			}
			else
			{
				isFirst = false;
			}
			sb.append("?");
		}
		sb.append(")");
		return sb.toString();
	}
	
	public static String generateSelectQuery(String tableName) throws UniDBFWException
	{
		List<String> columns = getColumns(tableName);
		StringBuilder sb = new StringBuilder("select * from ");
		sb.append(tableName);
		sb.append(" where ");
		boolean isFirst = true;
		for (String column: columns)
		{
			if (!isFirst)
			{
				sb.append(" and ");
			}
			else
			{
				isFirst = false;
			}
			sb.append(column);
			sb.append(" = ?");
		}
		return sb.toString();
	}
	
	public static String generateUpdateQuery(String tableName) throws UniDBFWException
	{
		List<String> columns = getColumns(tableName);
		StringBuilder sb = new StringBuilder("insert into ");
		sb.append(tableName);
		sb.append("(");
		boolean isFirst = true;
		for (String column: columns)
		{
			if (!isFirst)
			{
				sb.append(", ");
			}
			else
			{
				isFirst = false;
			}
			sb.append(column);
		}
		sb.append(") values( ");
		isFirst = true;
		for (String column: columns)
		{
			if (!isFirst)
			{
				sb.append(", ");
			}
			else
			{
				isFirst = false;
			}
			sb.append("?");
		}
		sb.append(")");
		return sb.toString();
	}
	
	private static List<String> getColumns(String tableName) throws UniDBFWException
	{
		List<String> columns = null;
		StringBuilder sb = new StringBuilder("select * from ");
		sb.append(tableName);
		sb.append(" where rownum <= 1");
		String query = sb.toString();
		Statement state = null; 
		try
		{
			state = ConnectionHelper.getConnection().createStatement();
			ResultSet rs = state.executeQuery(query);
			ResultSetMetaData meta = rs.getMetaData();
			columns = new ArrayList<String>(meta.getColumnCount());
			for (int i = 0; i < columns.size(); i++)
			{
				columns.add(i, meta.getColumnName(i));
			}
			return columns;
		}
		catch (SQLException e)
		{
			throw new UniDBFWException(e.getMessage(), e);
		}
		finally
		{
			ConnectionHelper.closeStatement(state);
		}
		
	}
	
}
